// pages/epi_4/audioGbam/audioGbamWork.js
var timer = require('../../../utils/timer')
Page({
  data: {
      state: 'running',
      music: {
        title: "第一首",
        src: "http://47.109.143.70/src/1.mp3"
      },
      play: {
        currentTime: '00:00',
        duration: '00:00',
        percent: 0,
        title: ''
      },
      msg: {
        onCanplay: '等待...',
        onPlay: '等待...',
        onPause: '等待...',
        onSeeking: '等待...',
        onSeeked: '等待...',
        onEnded: '等待...',
        onStop: '等待...'
      }
    },
    audioBam: null,
    onReady: function(){
      this.audioBam = wx.getBackgroundAudioManager()
      this.setMusic()
      this.audioBam.onError(()=> {
        console.log("播放失败:" + this.audioBam.src)
      })
      this.updateState()

      var that = this
      this.audioBam.onPlay(function(){
        that.setData({
          "msg.onPlay": "开始播放啦！" 
        })
      })

      this.audioBam.onPause(function(){
        that.setData({
          "msg.onPause": "暂停播放啦！" 
        })
      })

      this.audioBam.onCanplay(function(){
        that.setData({
          "msg.onCanplay": "可以开始播放啦！" 
        })
      })

      this.audioBam.onSeeking(function(){
        that.setData({
          "msg.onSeeking": "正在空降！" 
        })
      })

      this.audioBam.onSeeked(function(){
        that.setData({
          "msg.onSeeked": "空降成功！" 
        })
      })

      this.audioBam.onEnded(function(){
        that.setData({
          "msg.onEnded": "已经播放结束咯！" 
        })
      })

      this.audioBam.onStop(function(){
        that.setData({
          "msg.onStop": "播放被停止啦！" 
        })
        that.setMusic()
        that.audioBam.pause()
      })

    },

    play: function(){
      this.audioBam.play()
    },
    pause: function(){
      this.audioBam.pause()
    },
    jump: function(){
      var currentTime = this.audioBam.currentTime
      this.audioBam.seek(currentTime+10)
    },
    stop: function(){
      this.audioBam.stop()
      this.audioBam = this.audioBam
    },

    setMusic: function(){
      var music = this.data.music
      this.audioBam.src = music.src
      this.audioBam.title = music.title
      this.setData({
        'play.title': music.title,
        'play.currentTime': "00:00",
        'play.duration': "00:00",
        'play.percent': 0,
        state: 'running'
      })
    },

    updateState: function(){
      var updateTime = 0
      this.audioBam.onTimeUpdate(() =>{
        var currentTime = parseInt(this.audioBam.currentTime)
        if (!this.sliderChangeLock && currentTime !== updateTime){
          updateTime = currentTime
          this.setData({
            'play.duration': timer.convert(this.audioBam.duration || 0),
            'play.currentTime': timer.convert(currentTime),
            'play.percent': currentTime / this.audioBam.duration * 100
          })
        }
      })
    },

    sliderChangeLock: false,
    sliderChanging: function(e){
      var second = e.detail.value * this.audioBam.duration / 100
      this.sliderChangeLock = true
      this.setData({
        'play.currentTime': timer.convert(second),
      })
    },
    sliderChange: function(e){
      var second = e.detail.value * this.audioBam.duration / 100
      this.audioBam.seek(second)
      setTimeout(()=>{
        this.sliderChangeLock = false
      }, 1000)
    },

})